import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "Home" */ '../views/Home.vue'),
    children: [
      {
        path: '',
        name: 'index',
        component: () => import(/* webpackChunkName: "login" */ '../views/channels/index.vue')
      },
      {
        path: '/index/:catalog',
        name: 'catalog',
        component: () => import(/* webpackChunkName: "template" */ '../views/channels/template.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '/forget',
    name: 'forget',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "forget" */ '../views/Forget.vue')
  },
  {
    path: '/register',
    name: 'register',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "register" */ '../views/Register.vue')
  },
  {
    path: '/reset',
    name: 'reset',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "reset" */ '../views/Reset.vue')
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/user',
    name: 'user',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "user" */ '../views/user.vue')
  },
  {
    path: '/center',
    component: () => import(/* webpackChunkName: "center" */ '../views/center.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'center',
        component: () => import(/* webpackChunkName: "user-center" */ '../components/user/center.vue')
      },
      {
        path: 'setting',
        redirect: 'setting/info',
        component: () => import(/* webpackChunkName: "user-settings" */ '../components/user/settings.vue'),
        children: [
          {
            path: 'info',
            name: 'info',
            component: () => import(/* webpackChunkName: "my-info" */ '../components/user/common/myInfo.vue')
          },
          {
            path: 'account',
            name: 'account',
            component: () => import(/* webpackChunkName: "account" */ '../components/user/common/account.vue')
          },
          {
            path: 'pic',
            name: 'pic',
            component: () => import(/* webpackChunkName: "pic-upload" */ '../components/user/common/picUpload.vue')
          },
          {
            path: 'password',
            name: 'password',
            component: () => import(/* webpackChunkName: "password" */ '../components/user/common/password.vue')
          }
        ]
      },
      {
        path: 'message',
        name: 'message',
        component: () => import(/* webpackChunkName: "user-message" */ '../components/user/message.vue')
      },
      {
        path: 'post',
        redirect: 'post/post',
        component: () => import(/* webpackChunkName: "user-posts" */ '../components/user/posts.vue'),
        children: [
          {
            path: 'post',
            name: 'post',
            component: () => import(/* webpackChunkName: "user-my-post" */ '../components/user/common/myPost.vue')
          },
          {
            path: 'collection',
            name: 'collection',
            component: () => import(/* webpackChunkName: "user-collection" */ '../components/user/common/myCollection.vue')
          }
        ]
      },
      {
        path: 'other',
        name: 'other',
        component: () => import(/* webpackChunkName: "user-other" */ '../components/user/others.vue')
      }
    ],
    beforeEnter: (to, from, next) => {
      const isLogin = store.state.isLogin
      if (isLogin) {
        next()
      } else {
        next('/login')
      }
      next()
    }
  }
]

const router = new VueRouter({
  linkExactActiveClass: 'layui-this',
  routes
})

export default router
